package com.mxp.elm.mapper;

import com.mxp.elm.entity.UserEntity;
import com.mxp.elm.utils.PageUtil;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author mxp
 * @date 2023/2/23 11:18
 */
@Mapper
public interface UserMapper {

    /**
     * 新增用户
     * @param user
     * @return
     */
    @Insert("insert into tbl_user_info (userName, userPwd, userSex, userEmail, userBirthday, userType) values (#{userName}, md5(#{userPwd}), #{userSex}, #{userEmail}, #{userBirthday}, #{userType})")
    int save(UserEntity user);

    /**
     * 查询用户列表
     * @param page
     * @return
     */
    @Select("select * from tbl_user_info where userName like concat('%', ifnull(#{page.searchName},''), '%') order by userId desc limit #{start}, #{page.limit}")
    List<UserEntity> queryPage(@Param("page") PageUtil page, @Param("start") int start);

    /**
     * 根据ID查询用户信息
     * @param userId
     * @return
     */
    @Select("select userId, userName, userEmail, userSex, userBirthday from tbl_user_info where userId = #{userId}")
    UserEntity getUserById(@Param("userId") Integer userId);

    /**
     * 校验用户身份
     * @param loginName
     * @param loginPwd
     * @return
     */
    @Select("select * from tbl_user_info where (userName = #{loginName} or userEmail = #{loginName}) limit 1")
    UserEntity getUserByNameOrEmailAndPwd(@Param("loginName") String loginName);

    /**
     * 获取总记录数
     * @return
     */
    @Select("select count(*) from tbl_user_info")
    Integer count();

    /**
     * 通过id更新用户信息
     * @param user
     * @return
     */
    Integer updateUserById(UserEntity user);


    /**
     * 通过id删除用户
     * @param id
     * @return
     */
    @Update("update tbl_user_info set userStatus = userStatus ^ 1 where userId = #{id}")
    Integer modifyStatusById(Integer id);

    /**
     * 注册
     * @param user
     * @return
     */
    @Insert("insert into tbl_user_info(userName, userPwd) values (#{userName}, md5(#{userPwd}))")
    Integer insertUser(UserEntity user);

    /**
     * 修改密码
     * @param user
     * @return
     */
    @Update("update tbl_user_info set userPwd = md5(#{userPwd}) where userName = #{userName} limit 1")
    Integer resetPwd(UserEntity user);
}
